package com.freeSoft;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Properties;

public class CreateServiceImpl {
	private String tableName;
	private Properties prop;
 

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public Properties getProp() {
		return prop;
	}
	
	public void setProp(Properties prop) {
		this.prop = prop;
	}
	
	public void generateServiceImpl(){
		String serviceImplPath = prop.getProperty(Util.PROP_SERVICE_IMPL_PATH);
		String beanPath = prop.getProperty(Util.PROP_BEAN_PATH);
		String servicePath = prop.getProperty(Util.PROP_SERVICE_PATH);
		String outputDir = Util.getOutputDir(prop);
		String projactName = prop.getProperty(Util.PROJECT_NAME);
		String absolutePath = outputDir+ projactName + File.separator+ projactName+ Util.CORE_NAME +File.separator+ 
		"src"+File.separator+"main"+File.separator+"java"+File.separator+Util.javaPackToFilePath(serviceImplPath);
		File f = new File(absolutePath);
		if ( !f.exists()){
			f.mkdirs();
		}
 
		String beanName = tableName.substring(0, 1).toUpperCase()+tableName.substring(1, tableName.length());
		beanName = Util.changeName(beanName);
 
		File file = new File(absolutePath+File.separator+beanName+"ServiceImpl.java");
		try {
			PrintWriter out	   = new PrintWriter(new BufferedWriter( new FileWriter(file)));
			out.println("package "+serviceImplPath +";\r\n");
			out.println();
			out.println("import java.util.List;");
			out.println("import java.util.ArrayList;");
			out.println();
			beanName = Util.changeName(beanName);
			out.println("import "+beanPath+"."+beanName+";");
			out.println("import "+ servicePath+"."+beanName+"Service;");
			out.append(createBody(beanName));
			out.println("}");
			out.flush();
			out.close();
		}catch(Exception e){
			
		}
	}
 
	private StringBuffer createBody( String beanName ){
		StringBuffer bodyBuf = new StringBuffer();
		bodyBuf.append("	private "+beanName+"Dao "+ Util.changeInitialToLower(beanName)+"Dao;");	
		bodyBuf.append("	public void set"+beanName+"Dao("+beanName+"Dao "+ Util.changeInitialToLower(beanName)+"Dao){\r\n");
		bodyBuf.append("		this."+Util.changeInitialToLower(beanName)+"Dao = "+Util.changeInitialToLower(beanName)+"Dao;\r\n");
		bodyBuf.append("	}\r\n");
		
		bodyBuf.append("	public int delete"+beanName+"(int keyValue){\r\n");
		bodyBuf.append("		return "+Util.changeInitialToLower(beanName)+"Dao.delete"+ beanName+"(keyValue);\r\n");
		bodyBuf.append("	}\r\n");
		
		bodyBuf.append("	public int update"+beanName+"("+beanName+" "+ Util.changeInitialToLower(beanName)+" ){\r\n");
		bodyBuf.append("		return "+Util.changeInitialToLower(beanName)+"Dao.update"+beanName+"( "+Util.changeInitialToLower(beanName)+");\r\n");
		bodyBuf.append("	}\r\n");
		
		bodyBuf.append("	public int insert"+beanName+"("+beanName+" "+ Util.changeInitialToLower(beanName)+" ){\r\n");
		bodyBuf.append("		return "+Util.changeInitialToLower(beanName)+"Dao.update"+beanName+"( "+Util.changeInitialToLower(beanName)+");\r\n");
		bodyBuf.append("	}\r\n");
		
		bodyBuf.append("	public List<"+beanName+"> get"+beanName+"ById(int keyValue){\r\n");
		bodyBuf.append("		return "+Util.changeInitialToLower(beanName)+"Dao.get"+ beanName+"ById(keyValue);\r\n");
		bodyBuf.append("	}\r\n");
		
		bodyBuf.append("	public List<"+beanName+"> getAll"+beanName+"(int keyValue){\r\n");
		bodyBuf.append("		return "+Util.changeInitialToLower(beanName)+"Dao.getAll"+ beanName+"(keyValue);\r\n");
		bodyBuf.append("	}\r\n");
		return bodyBuf;
	}
}
